package com.vclee.fast.utils.task;


import java.util.Calendar;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Author: sir. Li
 * email:  lesli2@qq.com
 * Date:   2020/12/24
 */
public class CuxExecutors {

    /**
     * 获取延时的毫秒数
     * @param hours 首次执行时 24小时制(0-23)
     * @param minute 首次执行分
     * @return 延时的毫秒数
     */
    public static long regularFirstTime(Integer hours,Integer minute){
        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DATE,1);
        calendar.set(Calendar.HOUR_OF_DAY, hours);//时
        calendar.set(Calendar.MINUTE, minute);//分
        calendar.set(Calendar.SECOND, 0);//秒
        long preTime = calendar.getTime().getTime()-System.currentTimeMillis();
        return preTime>0? preTime:3600*24*1000+preTime;
    }

    public static ThreadPoolExecutor newFixedThreadPool(String name) {
        int nThreads = Runtime.getRuntime().availableProcessors();
        return newFixedThreadPool(nThreads, name);
    }

    public static ThreadPoolExecutor newFixedThreadPool(Integer nThreads, String name) {
        return new ThreadPoolExecutor(nThreads, nThreads, 0,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new NamedThreadFactory(name));
    }

    public static ThreadPoolExecutor newSingleFixedThreadPool(String name) {
        return new ThreadPoolExecutor(1, 1, 0,
                TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(), new NamedThreadFactory(name));
    }

    public static ScheduledThreadPoolExecutor newScheduledThreadPool(String name) {
        return new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), new NamedThreadFactory(name));
    }

    public static ScheduledThreadPoolExecutor newScheduledThreadPool(Integer nThreads,String name) {
        return new ScheduledThreadPoolExecutor(nThreads, new NamedThreadFactory(name));
    }

    public static ScheduledThreadPoolExecutor newSingleScheduledThreadPool(String name) {
        return new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(name));
    }
}
